//
//  DnHorizontalMenuTableCell.m
//  DnRollTabbarMenu
//
//  Created by Ngoc on 27/11/2011.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "DnRollHorizontalMenuTableCell.h"
#import "DnRollMenuConstant.h"
#import "DnRollMenuItem.h"

@implementation DnRollHorizontalMenuTableCell
@synthesize   distance, dataSource =_dataSource;

-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    //return [menuItems count];
    return [self.dataSource numberItemsOfHorizontalMenu:self];
}
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
-(UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath   
{
    
//    DnRollMenuItem *cell ;
//    if (cell==nil) {
//    }
//    cell = [menuItems objectAtIndex:indexPath.row];
//    return  cell;
    DnRollMenuItem *item = (DnRollMenuItem*)[self.dataSource horizontalMenu:self itemAtIndex:indexPath.row];
    return item;
}
-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{    
    DnRollMenuItem *cell ;
    cell =(DnRollMenuItem *) [self.dataSource horizontalMenu:self itemAtIndex:indexPath.row];
    NSInteger returnValue = cell.internalView.frame.size.width;
    if (cell!=nil && distance>0) {
        if ([cell.internalView isKindOfClass:[UIButton class]]) {
            UIButton * button = (UIButton*)cell.internalView;
            CGSize size =  [button.titleLabel.text sizeWithFont:button.titleLabel.font];
            returnValue = size.width+distance;
            if (indexPath.row == [self.dataSource numberItemsOfHorizontalMenu:self]-1 ) {
                returnValue +=distance;
            }
        }
        return returnValue<kMenuItemWidth?kMenuItemWidth:returnValue;
    }
    return  returnValue;
}

//-(void) addMenuItem:(DnRollMenuItem *)item
//{
//    [self addMenuItem:item atIndex:[menuItems count]];
//}
//-(void) addMenuItem:(DnRollMenuItem *)item atIndex:(NSInteger)menuIndex
//{
//    NSMutableArray *array = [menuItems mutableCopy];
//    [array insertObject:item atIndex:menuIndex];
//    menuItems = [NSArray arrayWithArray:array];
//}
//-(DnRollMenuItem*) menuItemAtIndex:(NSInteger)menuIndex 
//{
//    return (DnRollMenuItem*)[menuItems objectAtIndex:menuIndex];
//}
-(id) initWithFrame:(CGRect)frame
{
    if ((self=[super initWithFrame:frame])) {
        horizontalTableView =[[[UITableView alloc]initWithFrame:CGRectMake(0, 0, kMenuItemHeight, kMenuTableLength)]autorelease];
        horizontalTableView.showsVerticalScrollIndicator= NO;
        horizontalTableView.showsHorizontalScrollIndicator = NO;
        horizontalTableView.transform = CGAffineTransformMakeRotation(-M_PI*0.5);
        [horizontalTableView setFrame:CGRectMake(0, 0, kMenuTableLength-kMenuRowHorizontalPadding, kMenuItemHeight)];
        horizontalTableView.rowHeight = kMenuItemWidth;
        horizontalTableView.separatorColor = [UIColor clearColor];
        horizontalTableView.dataSource = self;
        horizontalTableView.delegate = self;
        horizontalTableView.autoresizingMask =  UIViewAutoresizingFlexibleWidth;
        horizontalTableView.backgroundColor =[UIColor clearColor];
        [self addSubview:horizontalTableView];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        distance = kMenuDistanceBetweenItem;
    }
    return self;
}
- (void)dealloc
{
    [super dealloc];
}

@end
